package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Device;
import com.sl.au.entity.ReplaceDevicePrice;
import com.sl.au.entity.ReplacementDevice;
import com.sl.au.entity.SinglePump;
import com.sl.au.entity.WholeSetDevice;
import com.sl.au.entity.WholeSetPrice;
import com.sl.au.repository.ReplaceDevicePriceRepository;
import com.sl.au.repository.ReplacementDeviceRepository;
import com.sl.au.repository.WholeSetDeviceRepository;
import com.sl.au.repository.WholeSetPriceRepository;
import com.sl.au.service.ExcelService;
import com.sl.au.service.ReplacementDeviceService;
import com.sl.au.service.WholeSetDeviceService;


/**
 * 成套设备管理
 * @author XH
 *
 */
@Service("WholeSetDeviceService")
@Transactional
public class WholeSetDeviceServiceImpl extends BaseServiceImpl implements WholeSetDeviceService {

	@Autowired
	WholeSetDeviceRepository wholeSetDeviceRepository;

	@Autowired
	WholeSetPriceRepository wholeSetPriceRepository;
	
	@Autowired
	ExcelService excelService;

	@Override
	public WholeSetDeviceRepository getRepository() {
		return this.wholeSetDeviceRepository;
	}

	@Override
	public List<WholeSetDevice> getPage(int start, int length, String key, String orderName, String dir) {
		Page<WholeSetDevice> pages=null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages=wholeSetDeviceRepository.getwholeSet(key, pageable);
		return pages.getContent();
	}

	@Override
	public void patch(WholeSetDevice wholeSetDevice) {
		WholeSetDevice w=this.getRepository().findOne(wholeSetDevice.getId());
		if(null==w) return;
		BeanUtils.copyProperties(wholeSetDevice, w, "createTime");
		this.getRepository().save(w);
		String type=w.getType();
		String setId=w.getId();
		wholeSetPriceRepository.updateTypeBywSetId(type, setId);
		
	}
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public long importExcel(String fileName) {
		long count = 0;
		if (!StringUtils.hasText(fileName))
			return count;
		Map<String, Object> collections = new HashMap<>();
		excelService.importExcel("wholeSet", collections, fileName);
		if (collections.size() < 1 || collections.get("items") == null)
			return count;
		List<WholeSetDevice> rows = (List<WholeSetDevice>) collections.get("items");
		List<WholeSetDevice> devices=this.getCheckedReDevice(rows);
		List<WholeSetPrice> prices = new ArrayList<WholeSetPrice>();
		if (null != devices && devices.size() > 0) {
			for(WholeSetDevice r:devices){
				String type = r.getType();
				WholeSetPrice p1 = new WholeSetPrice();
				p1.setWholeSetDevice(r);
				p1.setEquipmentType(type);
				p1.setSuperior("成套处");
				prices.add(p1);
				WholeSetPrice p2 = new WholeSetPrice();
				p2.setWholeSetDevice(r);
				p2.setEquipmentType(type);
				p2.setSuperior("外贸处");
				prices.add(p2);
				WholeSetPrice p3 = new WholeSetPrice();
				p3.setWholeSetDevice(r);
				p3.setEquipmentType(type);
				p3.setSuperior("销售处");
				prices.add(p3);
				WholeSetPrice p4 = new WholeSetPrice();
				p4.setWholeSetDevice(r);
				p4.setEquipmentType(type);
				p4.setSuperior("售后服务处");
				prices.add(p4);
			}
            this.getRepository().save(devices);        
            wholeSetPriceRepository.save(prices);
			count = devices.size();
		}
		return count;
	}

	@Override
	public List<WholeSetDevice> getCheckedReDevice(List<WholeSetDevice> rows) {
		List<WholeSetDevice> devices=new ArrayList<WholeSetDevice>();
		for(WholeSetDevice d:rows){
			String type=d.getType();
			long count=this.getRepository().getReCount(type);
			if(count==0){
				devices.add(d);
			}
		}
		return devices;
	}

	
}
